package com.wyq.action;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.json.JSONUtil;

import com.opensymphony.xwork2.ActionSupport;
import com.wyq.service.AudienceQService;
import com.wyq.service.AudienceService;

/**
 * 获取用户注册的调查问卷信息
 * @author Fyj
 *
 */
public class AdminGetQInfoAction extends ActionSupport {
	private AudienceQService audienceQService;
	private AudienceService audienceService;
	private String jsonStr;
	private String audienceId;

	@Override
	public String execute() throws Exception {
		HttpServletResponse response=ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		List audienceQInfo = audienceService.getAudienceQInfo(audienceId);
		String jsonData = "{'qtitleidarr':'420,421,422,423,424,425,','checksel':[{'qtitleid':'420','qidstr':'831*830*'},{'qtitleid':'421','qidstr':'833*832*'},{'qtitleid':'422','qidstr':'838*'},{'qtitleid':'423','qidstr':'841*'},{'qtitleid':'424','qidstr':'844*'},{'qtitleid':'425','qidstr':'846*'},]}"; 
		jsonData = ((Map)audienceQInfo.get(0)).get("qInfo").toString();
		//将问卷调查的信息转换为Map
		Map map = (Map)JSONUtil.deserialize(jsonData);
		 List qIDList = (List) map.get("checksel");
		// System.out.println("size:::" + qIDList);
		 //返回两个字符串，一个是大类的题目和ID,另一个是题目ID和对应的小类的题目'，'分隔的串
		 String qtitleInfo = "[";
		 String qInfo = "[{";
		 JSONArray jsonArr = JSONArray.fromObject(qIDList);
		 for(int i=0;i<jsonArr.size();i++){
			// System.out.println("check:::" + jsonArr.get(i).toString());
			 Map qIDMap = (Map)JSONUtil.deserialize(jsonArr.get(i).toString()); 
			 
			 //得到大标题的qTileID
			 String qTitleID = qIDMap.get("qtitleid").toString();
			 qtitleInfo += "{'qtitleid':'" + qTitleID + "',";
			 String qTitleText = audienceQService.getQTitleName(qTitleID); 
			 qtitleInfo += "'qtitlename':'" + qTitleText + "'},";
			 
			// System.out.println("&&&&&" + qIDMap.get("qidstr"));
			 
			 
			 String qIDStr = qIDMap.get("qidstr").toString();
			 String[] qIDArr = qIDStr.split("[*]");//此处写"*"会报错，因正则表达式的关系，要加上"[*]"
			 qInfo += "'" + qTitleID + "':'";
			 for(int j=0;j<qIDArr.length;j++){
				 String qtext = "";
				 if(!qIDArr[j].equals("-1")){
					 qtext = audienceQService.getQText(qIDArr[j]);
				 }else{
					 qtext = qIDMap.get("otherval").toString();
				 }
				 qInfo += qtext + "，";
			 }
			 qInfo += "',";
		 }
		 qtitleInfo += "]";
		 qInfo += "}]";
		 
		 jsonStr = ("{'qtitleinfo':" + qtitleInfo + ",'qinfo':" + qInfo + "}").replaceAll(",]", "]");
		 jsonStr = jsonStr.replaceAll(",}", "}");
		 
		// System.out.println(jsonStr);
		 
		return SUCCESS;
		
	}

	public void setAudienceQService(AudienceQService audienceQService) {
		this.audienceQService = audienceQService;
	}
	
	public void setAudienceService(AudienceService audienceService) {
		this.audienceService = audienceService;
	}

	public String getJsonStr() {
		return jsonStr;
	}

	public void setAudienceId(String audienceId) {
		this.audienceId = audienceId;
	}

}
